<?php defined('RUN_KATANA') or die('Restricted access');
class ShopQuestionAnswer extends Object
{
	public $Title;
	public $Type;
	public $Active;
	public $Responses;
	public $ResponsesCount;
	
	public function __toString()
	{
		$out  = self::getTitle();
		return $out;
	}
	
	public function getType() { return intval($this->Type); }
	public function getActive() { return intval($this->Active); }
	public function getResponsesCount()
	{ 
		self::loadRensponses();
		return intval($this->ResponsesCount); 
	}
	public function getRensponses() 
	{ 
		self::loadRensponses();
		{ return $this->Rensponses; } 
	}
	
	public function setType($val) { $this->Type=intval($val); }
	public function setActive($val) { $this->Active=intval($val); }
	
	public function loadRensponses() 
	{
		global $db;
		if(self::getId() > 0)
		{
			$sql  = ' SELECT * FROM '.DB_PERFIX.'shop_question_responses WHERE answer_id='.self::getId().';';
			$data = $db->fetch($sql, CACHE_TTL);
			if(is_array($data))
			{
				foreach($data as $row)
				{
					$this->Responses[$row['id']] = new ShopQuestionResponse();
					$this->Responses[$row['id']]->setId($row['id']);
					$this->Responses[$row['id']]->setOrderId($row['order_id']);
					$this->Responses[$row['id']]->setQuestionId($row['question_id']);
					$this->Responses[$row['id']]->setAnswerId($row['answer_id']);
					$this->Responses[$row['id']]->setAnswerValue($row['answer_value']);
				}
				self::setResponsesCount(count($this->Responses));
			}
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'shop_question_answer SET ';
			$sql .= '`title`="'.self::formatText(self::getTitle()).'", ';
			$sql .= '`type`='.self::getType().', ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
}
?>